package b2.scene.fight
{
	import b2.net.*;
	
	import flash.display.BitmapData;
	import flash.display.Graphics;
	
	import org.flixel.*;

	public class TraceSprite extends FlxSprite
	{
//		//箭头参数
//		private const D:Number = PlayState.TILE_WIDTH * 0.1;
//		private const p1:Array = [[0, -D], [D, 0], [0, D], [-D, 0]];
//		private const p2:Array = [[D/2, D/2], [-D/2, D/2], [-D/2, -D/2], [D/2, -D/2]];
//		private const p3:Array = [[-D/2, D/2], [-D/2, -D/2], [D/2, -D/2], [D/2, D/2]];
		//圆形参数
		private const R:Number = PlayState.TILE_WIDTH * 0.1;
		
		public function TraceSprite()
		{
			super();
		}
		
		private function drawOneDirection(node:FlxPoint, direction:int, additionalLength:Number = 0):void
		{
			var l1:Number = PlayState.TILE_WIDTH * 0.4 + additionalLength;
			var l2:Number = PlayState.TILE_WIDTH * 0.2 + additionalLength;
			var w1:Number = PlayState.TILE_WIDTH / 12;
			var p1:Array = [[-w1, -l1], [l2, -w1], [-w1, +l1], [-l2, -w1],
			[-0.7 * l1 - 0.7 * w1, -0.7 * l1 + 0.7 * w1], [+0.7 * l1 + 0.7 * w1, -0.7 * l1 + 0.7 * w1],
			[+0.7 * l1 + 0.7 * w1, +0.7 * l1 - 0.7 * w1], [-0.7 * l1 - 0.7 * w1, +0.7 * l1 - 0.7 * w1]];
			var p2:Array = [[+w1, -l1], [l1, -w1], [+w1, +l1], [-l1, -w1],
			[-0.7 * l1 + 0.7 * w1, -0.7 * l1 - 0.7 * w1], [+0.7 * l1 - 0.7 * w1, -0.7 * l1 - 0.7 * w1], 
			[+0.7 * l1 - 0.7 * w1, +0.7 * l1 + 0.7 * w1], [-0.7 * l1 + 0.7 * w1, +0.7 * l1 + 0.7 * w1]];
			var p3:Array = [[+w1, -l2], [l1, w1], [+w1, +l2], [-l1, w1],
			[-0.7 * l2 + 0.7 * w1, -0.7 * l2 - 0.7 * w1], [+0.7 * l2 - 0.7 * w1, -0.7 * l2 - 0.7 * w1], 
			[+0.7 * l2 - 0.7 * w1, +0.7 * l2 + 0.7 * w1], [-0.7 * l2 + 0.7 * w1, +0.7 * l2 + 0.7 * w1]];
			var p4:Array = [[-w1, -l2], [l2, w1], [-w1, +l2], [-l2, w1],
			[-0.7 * l2 - 0.7 * w1, -0.7 * l2 + 0.7 * w1], [+0.7 * l2 + 0.7 * w1, -0.7 * l2 + 0.7 * w1], 
			[+0.7 * l2 + 0.7 * w1, +0.7 * l2 - 0.7 * w1], [-0.7 * l2 - 0.7 * w1, +0.7 * l2 - 0.7 * w1]];
			var gfx:Graphics = FlxG.flashGfx;
			gfx.beginFill(0x000000, 0.8);
			gfx.moveTo(node.x + p1[direction][0], node.y + p1[direction][1]);
			gfx.lineTo(node.x + p2[direction][0], node.y + p2[direction][1]);
			gfx.lineTo(node.x + p3[direction][0], node.y + p3[direction][1]);
			gfx.lineTo(node.x + p4[direction][0], node.y + p4[direction][1]);
			gfx.lineTo(node.x + p1[direction][0], node.y + p1[direction][1]);
			gfx.endFill();
		}
		
		private function drawOneNode(node:FlxPoint, previousDirection2:int, previousDirection:int, nextDirection:int, nextDirection2:int):void
		{
			//车轴
			for (var i:int = 0; i < 4; i++)
			{
				if (i != previousDirection2 && i != previousDirection && i != nextDirection && i != nextDirection2)
					drawOneDirection(node, i);
			}
			//拐弯
			if (previousDirection == 0 && nextDirection == 1 || previousDirection == 1 && nextDirection == 0)
			{
				drawOneDirection(node, 5, PlayState.TILE_WIDTH * 0.2);
				drawOneDirection(node, 7);
			}
			else if (previousDirection == 2 && nextDirection == 3 || previousDirection == 3 && nextDirection == 2)
			{
				drawOneDirection(node, 5);
				drawOneDirection(node, 7, PlayState.TILE_WIDTH * 0.2);
			}
			else if (previousDirection == 1 && nextDirection == 2 || previousDirection == 2 && nextDirection == 1)
			{
				drawOneDirection(node, 4);
				drawOneDirection(node, 6, PlayState.TILE_WIDTH * 0.2);
			}
			else if (previousDirection == 0 && nextDirection == 3 || previousDirection == 3 && nextDirection == 0)
			{
				drawOneDirection(node, 4, PlayState.TILE_WIDTH * 0.2);
				drawOneDirection(node, 6);
			}
//			//方向箭头
//			var gfx:Graphics = FlxG.flashGfx;
//			gfx.beginFill(0x000000, 0.8);
//			gfx.moveTo(node.x + p1[nextDirection][0], node.y + p1[nextDirection][1]);
//			gfx.lineTo(node.x + p2[nextDirection][0], node.y + p2[nextDirection][1]);
//			gfx.lineTo(node.x + p3[nextDirection][0], node.y + p3[nextDirection][1]);
//			gfx.lineTo(node.x + p1[nextDirection][0], node.y + p1[nextDirection][1]);
//			gfx.endFill();
		}
		
		private function drawCircle(node:FlxPoint, color:uint):void
		{
			var gfx:Graphics = FlxG.flashGfx;
			gfx.beginFill(color, 0.8);
			gfx.drawCircle(node.x, node.y, R);
			gfx.endFill();
		}
		
		private function getDirection(node1:FlxPoint, node2:FlxPoint):int
		{
			if (node1 == null || node2 == null)
				return -1;
			var diffX:Number = node2.x - node1.x;
			var diffY:Number = node2.y - node1.y;
			if (Math.abs(diffY) > Math.abs(diffX) && diffY < 0)
				return 0;
			else if (Math.abs(diffX) > Math.abs(diffY) && diffX > 0)
				return 1;
			else if (Math.abs(diffY) > Math.abs(diffX) && diffY >= 0)
				return 2;
			else 
				return 3; 
		}
		
		public function drawPath(path:FlxPath):void
		{
			if (path == null || path.nodes.length == 0)
				return;
			pixels = new BitmapData(PlayState.OFFSET_X + PlayState.TILE_NUM * PlayState.TILE_WIDTH, PlayState.OFFSET_Y + PlayState.TILE_NUM * PlayState.TILE_WIDTH, true, 0);
			//Draw line
			var gfx:Graphics = FlxG.flashGfx;
			gfx.clear();
			for (var i:int = 0; i < path.nodes.length; i++)
			{
				var node:FlxPoint = path.nodes[i];
				var nextNode:FlxPoint = i + 1 < path.nodes.length ? path.nodes[i + 1] : null;
				var nextNode2:FlxPoint = i + 2 < path.nodes.length ? path.nodes[i + 2] : null;
				var previousNode:FlxPoint = i - 1 >= 0 ? path.nodes[i - 1] : null;
				var previousNode2:FlxPoint = i - 2 >= 0 ? path.nodes[i - 2] : null;
				var previousDirection2:int = getDirection(previousNode, previousNode2);
				var previousDirection:int = getDirection(node, previousNode);
				var nextDirection:int = getDirection(node, nextNode);
				var nextDirection2:int = getDirection(nextNode, nextNode2);
				if (previousDirection == -1)
					previousDirection = (nextDirection + 2) % 4;
				if (nextDirection == -1)
					nextDirection = (previousDirection + 2) % 4;
				drawOneNode(node, previousDirection2, previousDirection, nextDirection, nextDirection2);
			}
			drawCircle(path.nodes[0], 0x00FF00);
			drawCircle(path.nodes[path.nodes.length - 1], 0xFF0000);
			//Cache line to bitmap
			pixels.draw(FlxG.flashGfxSprite);
			dirty = true;
		}
		
	}
}